package servlets;

import aggregate.aggregator.remote.ClientRemoteImpl;
import dao.ContentStatisticEntityFacadeLocal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;

import entities.ContentEntity;


import dao.RateEntityFacadeLocal;
import entities.ContentStatisticEntity;
import entities.RateEntity;
import entities.UserEntity;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;


/**
 * 
 * @author andrei costache
 *
 */
public class RateContentController extends ContentController {
	
	@EJB
        public RateEntityFacadeLocal rateDAO;
        @EJB ContentStatisticEntityFacadeLocal statisticDAO;

	public void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            RequestDispatcher view = null;
            String cid = request.getParameter("cid");
            String redirect = null;
            if (cid != null) {
                ContentEntity content = contentDAO.find(new Long(cid));
                HttpSession session = request.getSession();
                Long l_uid = ((UserEntity)session.getAttribute("user")).getUid();
                Integer l_cid = Integer.parseInt(cid);
                RateEntity rating = rateDAO.findByUidAndCid(l_uid, new Long(l_cid));
                //Check if a new comment has to be stored.
                String submitted = request.getParameter("submitted");
                if (submitted != null){
                    String rate = request.getParameter("rate");
                    boolean ok = true;
                    if (rate == null) {
                        ok = false;
                        msg.addStatusMessage("You need to re-rate the content");
                    }
                    if (ok){
                        if (rating == null) {
                            rating = new RateEntity();
                            rating.setUid(l_uid);
                            rating.setCid(new Long(l_cid));
                            rating.setCreated(System.currentTimeMillis()/1000);
                            rating.setRate_value(Double.parseDouble(rate));
                            rateDAO.create(rating);
                            //Announce the server.
                            //ClientRemoteImpl client = ClientRemoteImpl.getInstance();
                            //client.ratingAdded(l_cid, Double.parseDouble(rate));
                        } else {
                            rating.setRate_value(Double.parseDouble(rate));
                            rateDAO.edit(rating);
                        }
                        double rate_avg = rateDAO.calculateAvgRate(new Long(l_cid));
                        ContentStatisticEntity c_stat = statisticDAO.findByContentId(new Long(l_cid));
                        if (c_stat != null){
                            c_stat.setRating(rate_avg);
                            statisticDAO.edit(c_stat);
                            request.setAttribute("statistic", c_stat);
                        }
                        //msg.addStatusMessage("Content has been rated");
                        //redirect = "/content/view?cid="+cid;
                    }
                    request.setAttribute("messages", msg);
                    view = request.getRequestDispatcher("/jsp/fragments/RateContent.jsp");
                } else {
                    view = request.getRequestDispatcher("/jsp/fragments/RateContent.jsp");
                }
            if (redirect != null){
                response.sendRedirect(request.getContextPath()+redirect);
            } else {
                    //Inject the content and the comments.
                request.setAttribute("content", content);
                request.setAttribute("messages", msg);
                request.setAttribute("rating",rating);
                view.forward(request, response);
            }

        }
    }
}
